// pages/address/edit.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['广东省', '广州市', '海珠区'],
    address:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id ? options.id : 0;
    if(id != 0){
      var that = this;
      var data = {};
      data.openid = wx.getStorageSync("openid");
      data.session = wx.getStorageSync("session");
      data.id=id;
      util.request('/api-user/get-address-by-id', data, function (data) {
        that.setData({
          region:data.region,
          address: data.data,
        })
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  formSubmit:function(e){
    console.log(e);
    var data = {};
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    data.openid=wx.getStorageSync("openid");
    data.session=wx.getStorageSync("session");
    data.id = this.data.address.id ? this.data.address.id:'';
    data.name = e.detail.value.name;
    if (!myreg.test(e.detail.value.tel)) {
      wx.showToast({
        title: '手机号不正确',
        icon: "none"
      })
      return;
    } 
    data.tel = e.detail.value.tel;
    data.region = e.detail.value.region;
    data.detail = e.detail.value.detail;
    util.request('/api-user/add-address', data, function (data) {
      if(data.status){
          wx.redirectTo({
            url: '../address/list',
          })
      }else{
        wx.showToast({
          title: data.msg,
          icon: none,
          duration: 2000
        })
      }
    });
  },

})